$(function(){
	readyclass("addclass-select");
	readyClass();
	readystudent();
	readyteacher();
	$("#submit1").click(function(){
		/**
		 * 获取数据
		 */
		 var Class = $("#addclass-select").val();
		 var teacher = $("#addteacher-select").val();
         var subject =  $("#addsubject-select").val();
	})
	$("#submit2").click(function(){ 
		 /**
         * 获取时间
         */
        var oDate = new Date(); //实例一个时间对象；
        var year= oDate.getFullYear(); //获取系统的年；
        var morth=oDate.getMonth()+1; //获取系统月份，由于月份是从0开始计算，所以要加1
        var day=oDate.getDate(); // 获取系统日，
        var q = year+"."+morth+"."+day;
		table(q);
	});
	
})
var student={};
function readystudent(){
	$.ajax({
		url : "/blackhouse/studentinfo",
		data:{
			'code':"4"
		},
		type : "post",
		dataType : "json",
		async:false,
		success : function(e) {
			student=e;
		}
	})
	
}
var Teacher={};//存储教师信息的
function readyteacher(){
	$.ajax({
		url : "/blackhouse//read_teacherServlet",
		type : "post",
		data:{"type":1},
		dataType : "json",
		async:false,
		success : function(e) {
			var html = "";
 			for(var i = 0;i<e.length;i++)
 			{
 				html+="<option value='" + e[i].id + "'>"+e[i].tName+"</potion>"
 			}
 			$("#teacher").html(html);
		}
	})
}

var Class={};
function readyClass(){
	$.ajax({
		url : "/blackhouse/seeclass",
		type : "post",
		dataType : "json",
		async:false,
		success : function(e) {
			Class=e;
		}
	})
}
	
var Subject = {};
function readySubject(){
		 $.ajax({
		 		url : "/blackhouse/subject",
		 		type : "post",
		 		data:{"code":10},
		 		dataType : "json",
		 		success : function(e) {
		 			var html = "";
		 			for(var i = 0;i<e.length;i++){
		 				html += "<option value='" + e[i].id + "'>" + e[i].subjectname
						+ "</option>"
		 			}
		 			$("#subject").html(html);
		 		}
		 })
	 }
	

function table(q){
	var className = $("#addclass-select option:selected").text();
	var strVar="";
	for(var i=0;i<Class.length;i++){
		if(Class[i].className==className){
			strVar += "<tr>";
			strVar += "<td>"+Class[i].className+"<\/td>";
			strVar += "<td>"+q+"<\/td>";
			var num=0;
			for(var j= 0;j<student.length;j++){
				if(student[j].stuclassid==className){
					num++;
				}
			}
			strVar += "<td>"+num+"<\/td>";	
			strVar += "<td> <select class=\"form-control\"  id=\"teacher\"></select></td>";
			strVar += "<td> <select class=\"form-control\"  id=\"subject\"></select></td>";
			strVar += "<td><button type=\"button\" class=\"btn btn-default  col-sm-offset-1\" id=\"bc\">保存</button></td><\/td>";
			strVar += "<\/tr>";
		}
	}
	$("#tbody").html(strVar);
	readyteacher();
	readySubject();
	$("#bc").click(function(){ 
		SubmitTeacher();
	});
	
}
/**
 * 保存班级分配到的教师
 * **/
function SubmitTeacher(){
	var List = new Array();
	// 遍历每一个tr
	$("#tbody").find("tr").each(function(){
		var classId = $("#addclass-select option:selected").val();//拿取当前tr的
		var tId = $("#teacher option:selected").val();
		var subId = $("#subject option:selected").val();
		$.ajax({
			url : "/blackhouse/read_teacherServlet",
			type : "post",
			data:{"type":2,
				"tId":tId,
				"classId":classId,
				"subId":subId},
			dataType : "json",
			success : function(e) {
				if (e!=-1) {
					swal('添加信息成功', '谢谢使用', 'success');
				} else {
					swal('添加信息失败', '未知错误', 'erreo');
				}
			}
	    });
    })  
}
